Skip to content

gh-138864: Improve SyntaxError message for case with expression - #138865

Open
sobolevn wants to merge 6 commits into
python:mainfrom
sobolevn:issue-138864
Open

sobolevn wants to merge 6 commits into
python:mainfrom
sobolevn:issue-138864

Conversation

@sobolevn

@sobolevn sobolevn commented Sep 13, 2025 •

Copy link
Copy Markdown
Member
Снимок экрана 2025-09-13 в 12 53 17

In my opinion this makes already awesome python error messages (thanks, @pablogsal!) even cooler.

Maybe there are some hidden things that I've missed? I would be happy to fix them :)
In case this complicates our parser quite a bit - feel free to close.

Comment thread Grammar/python.gram Resolved

@ilovelinux ilovelinux left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think some people may be using case ...: to write case _:

Do we want to add this suggestion to the error message?

@sobolevn

sobolevn commented Sep 15, 2025 •

Copy link
Copy Markdown
Member Author

Nope, sorry :)
It is not really 100% expected, people might also mean case Ellipsis: :)

Comment thread Grammar/python.gram
| a="case" patterns guard? ':' NEWLINE !INDENT {
RAISE_INDENTATION_ERROR("expected an indented block after 'case' statement on line %d", a->lineno) }
invalid_case_pattern:
| "case" a=expression guard? ':' block {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it would be better to use this variant?

"case" a=expression 'as'? expression? guard? ':' block

For cases like this:

match ...:
  case T[1] as E:
    print(1)

@pablogsal pablogsal Oct 6, 2025 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be slower and a bit more unreadable I think and also allows to match "case" a=expression 'as' guard ':' block which makes no sense.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can run for ('as' expression)?. But "slower" sounds ugly, maybe such a little change doesn't worth it.

@sergey-miryanov

Copy link
Copy Markdown
Contributor

Is there anything else that needs to be done?

@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting core review stale Stale PR or inactive for long period of time.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants